from selenium.webdriver import Chrome
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

# webdriver = Chrome(executable_path='/Users/liuqi/Downloads/chromedriver')

# 提取、保存数据
# webdriver.get('http://www.xbiquge.la/xiaoshuodaquan/')
# items = webdriver.find_elements_by_xpath("//div[@class='novellist']/ul/li/a")
# with open("/Users/liuqi/Desktop/xiaoshuo1.txt", "w") as f:
#     for i in items:
#         novel_name = i.text
#         novel_url = i.get_attribute("href")
#         # print(novel_name, novel_url)
#         f.write("novel_name:{},novel_url:{}".format(novel_name, novel_url) + "\n")
# print("done!")
# webdriver.close()

# 模拟input输入、提交
# webdriver.get('https://www.douban.com/')
# search_input = webdriver.find_element_by_xpath("//*[@id='anony-nav']/div[2]/form/span[1]/input")
# search_input.send_keys("音乐")
# search_input.submit()
# time.sleep(3)
# webdriver.close()

# 点击
# webdriver.get('https://www.douban.com/')
# point = webdriver.find_element_by_xpath("//*[@id='anony-nav']/div[1]/ul/li[2]/a")
# point.click()
# time.sleep(3)
# webdriver.close()


# 等待
# webdriver = Chrome(executable_path='/Users/liuqi/Desktop/chromedriver')
driver = Chrome(executable_path='/Users/liuqi/Desktop/chromedriver')
driver.get("https://movie.douban.com/")

try:
    # # 构造wait对象
    # wait = WebDriverWait(driver, 10)
    # # 构造等待条件
    # condition = expected_conditions.presence_of_element_located((By.CLASS_NAME, 'item'))
    # # 等待直到指定条件发生
    # WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, 'item')))
    # # 提取数据
    # for i in driver.find_element_by_class_name('item'):
    #     print(i)

    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, 'item')))
    for i in driver.find_elements_by_class_name('item'):
        print(i)
finally:
    driver.quit()


